package com.cs.dingdingauto.extent;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.core.app.NotificationCompat;
import android.widget.RemoteViews;
import com.cs.dingdingauto.R;
import java.util.concurrent.TimeUnit;


public class ExternalNotifyUtil {
    public static final long sDelayed = TimeUnit.SECONDS.toMillis(1);
    public static final Handler sHandler = new MyHandler(Looper.getMainLooper());
    public static final int MEDIA_INFO_PLAY_TO_END = 10101;
    private static Context sApplication;

    private static class MyHandler extends Handler {
        public MyHandler(Looper looper) {
            super(looper);
        }

        public void handleMessage(Message message) {
            super.handleMessage(message);
            if (message.what == 101) {
                ExternalNotifyUtil.handle101(sApplication);
            }
        }
    }

    private static void initChannel(NotificationManager notificationManager) {
        if (Build.VERSION.SDK_INT >= 26 &&
                notificationManager.getNotificationChannel("comm_external_op_ntf_chn_id") == null) {
            NotificationChannel notificationChannel = new NotificationChannel("comm_external_op_ntf_chn_id", "天气", NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setDescription("如暴雨、沙尘暴等");
            notificationChannel.setLockscreenVisibility(-1);
            notificationChannel.enableLights(false);
            notificationChannel.enableVibration(false);
            notificationChannel.setShowBadge(false);
            notificationChannel.setSound(null, null);
            notificationChannel.setBypassDnd(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }

    public static void sendNotify(Context context, PendingIntent pendingIntent) {
        if (sApplication == null) {
            sApplication =  context.getApplicationContext();
        }
        try {
            NotificationManager notificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            initChannel(notificationManager);
            notificationManager.cancel("AA_TAG1", MEDIA_INFO_PLAY_TO_END);
            notificationManager.notify("AA_TAG1", MEDIA_INFO_PLAY_TO_END,
                    new NotificationCompat.Builder(context, "comm_external_op_ntf_chn_id")
                            .setSmallIcon(R.drawable.comm_ad_close)
                            .setFullScreenIntent(pendingIntent, true)
                            .setCustomHeadsUpContentView(
                                    new RemoteViews(context.getPackageName(), R.layout.layout_external_notify))
                            .setPriority(NotificationCompat.PRIORITY_MAX).setCategory(Notification.CATEGORY_CALL)
                            .setOngoing(true)
                            .build());
            sHandler.removeMessages(101);
            sHandler.sendEmptyMessageDelayed(101, sDelayed);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void handle101(Context context) {
        cancelNotify((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
    }

    private static void cancelNotify(NotificationManager notificationManager) {
        try {
            notificationManager.cancel("AA_TAG1", MEDIA_INFO_PLAY_TO_END);
        } catch (Exception unused) {
            unused.printStackTrace();
        }
    }
}